package com.jsd.jmm;

import java.util.concurrent.atomic.AtomicInteger;

/**
 * volatile
 * 1.保证可见性
 * 2.不保证原子性
 * 3.禁止指令重排
 */
public class VolatileDemo {

    static AtomicInteger num = new AtomicInteger();

    public static void main(String[] args) {

        for (int i = 0; i < 20; i++) {
            new Thread(() -> {
                for (int j = 0; j < 1000; j++) {
                    add();
                }
            }).start();
        }

        while (Thread.activeCount() >2 ){
            Thread.yield();
        }

        System.out.println(num);
    }

    private static void add() {
        num.getAndIncrement();
    }
}
